<template>
	<view class='scan-success df-yc'>
		<!-- 导航栏 -->
		<view class="head_box" :style="{ background: bgcolor }">
			<cu-custom :isBack="true" :showBackIcon="true">
				<block slot="backText">
					<text class="nav-title shopro-selector-rect">扫码成功</text>
				</block>
			</cu-custom>
		</view>
		<view class="scroll_wrap">
			<scroll-view scroll-y="true">
				<box :scanData="scanData" v-if="type=='box'"></box>
				<jar :scanData="scanData" v-if="type=='pot'"></jar>
				<view class="slogan df-c mt30">
					<shopro-slogan color="#999999"></shopro-slogan>
				</view>
				<view class="tabbar df-c">
					<view class="scan" @tap="openScan">
						<image class="image" :src="temImgUrl+'/scan.png'" mode="widthFix"></image>
						继续扫码添加
					</view>
				</view>
			</scroll-view>
		</view>
		<view v-if="scanData.bowl.bowl_pop||scanData.bowl.merge_pop" class="popup wh100">
			<popup @changeType="changeType" @updateScanData="update" @close='close' :bowlData="scanData.bowl"
				:Poptype="Poptype">
			</popup>
		</view>
	</view>
</template>

<script>
	import box from "../components/scan-box-success.vue"
	import jar from '../components/scan-jar-success.vue'
	import popup from '../components/popup.vue'
	export default {
		components: {
			box,
			jar,
			popup
		},
		data() {
			return {
				bgcolor: '#F0F0F0',
				scanAllData: {
					bowlPop: false,
					mergePop: false,
					bowlData: {},
				},
				type: "",
				Poptype: 'all',
				scanData: {},
				temImgUrl: this.$IMG_URL_TEMP,

			};
		},
		onLoad(options) {
			this.type = options.type
			this.scanData = JSON.parse(options.data)
			if (this.scanData.bowl.bowl_pop) {
				this.Poptype = 'all'
			}
			if (!this.scanData.bowl.bowl_pop && this.scanData.bowl.merge_pop) {
				this.Poptype = 'merge'
			}
			if (!this.scanData.bowl.bowl_pop && !this.scanData.bowl.merge_pop) {
				this.Poptype = ''
			}
			this.setShareInfo({
				query: {
					id: this.scanData.pro_id
				},
				path: '/pages/goods/detail/index',
				title: '我分享了' + this.scanData.pro_name,
				image: decodeURIComponent(this.scanData.open_teabox_img)
			})
		},
		methods: {
			jump(path, params) {
				this.$Router.push({
					path: path,
					params: params
				})
			},
			changeType(data) {
				this.Poptype = data
			},
			update(data) {
				this.scanData = data
				this.type = data.pro_type
				console.log(data)
			},
			close(type) {
				if (type == 'all') {
					if (this.scanData.bowl.merge_pop) {
						this.scanData.bowl.bowl_pop = false
						this.Poptype = 'merge'
					} else {
						this.scanData.bowl.bowl_pop = false
					}
				} else if (type == 'merge') {
					this.scanData.bowl.merge_pop = false
				} else {
					this.scanData.bowl.bowl_pop = false
					this.scanData.bowl.merge_pop = false
				}
			},
			openScan() {
				let that = this
				uni.scanCode({
					success: function(res) {
						that.scan(that.codeParse(res.result))
					}
				});
			},
			scan(code) {
				uni.showLoading({
					title:"添加中",
					icon:"loading"
				})
				const that = this
				this.$api('tea.scanAdd', {
					code
				}).then(res => {
					if (res.code == 1) {
						uni.hideLoading()
						this.$tools.toast("扫码成功")
						this.scanData = res.data
						this.type = res.data.pro_type
						if (this.scanData.bowl.bowl_pop) {
							this.Poptype = 'all'
						}
						if (!this.scanData.bowl.bowl_pop && this.scanData.bowl.merge_pop) {
							this.Poptype = 'merge'
						}
						if (!this.scanData.bowl.bowl_pop && !this.scanData.bowl.merge_pop) {
							this.Poptype = ''
						}
					} 
					// else if (res.code == 0) {
					// 	setTimeout(function() {
					// 		uni.navigateBack()
					// 	}, 1000);
					// }
				})
			},
			// url解析code
			codeParse(url) {
				let codeStr = url.split('?')[1]
				let code = codeStr.split('=')[1]
				return code
			},
		}
	}
</script>

<style lang="scss" scoped>
	.slogan {
		margin-top: 60rpx;
		margin-bottom: 200rpx;
	}

	.popup {
		position: absolute;
		top: 0;
		left: 0;
	}

	.scan-success {
		width: 100vw;
		height: 100vh;
	}

	.scroll_wrap {
		overflow: hidden;
		width: 100%;
		flex: 1;

		scroll-view {
			width: 100%;
			height: 100%;
		}
	}

	.head_box {
		width: 750rpx;
	}

	.tabbar {
		position: fixed;
		width: 750rpx;
		bottom: 50rpx;
		left: 0;

		.scan {
			margin-top: 30rpx;
			width: 620rpx;
			height: 90rpx;
			background: #B83F46;
			border-radius: 45rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #FFFFFF;

			.image {
				width: 26rpx;
				margin-right: 16rpx;
			}
		}
	}
</style>
